package com.slipper.system.codeless.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.slipper.constants.Constants;
import com.slipper.system.codeless.bo.ViewFolderBO;
import com.slipper.system.codeless.dao.ViewFolderDao;
import com.slipper.core.transaction.ReadTransactional;
import com.slipper.core.transaction.WriteTransactional;
import com.slipper.util.Tool;
import com.slipper.util.UUID;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@ReadTransactional
public class ViewFolderService {
    @Resource
    private ViewFolderDao viewFolderDao;
    
    public ViewFolderBO queryBy(String id) {
        return viewFolderDao.queryBy(id);
    }
    
    public List<ViewFolderBO> queryList(ViewFolderBO bo) {
        return viewFolderDao.queryList(bo);
    }
    
    public PageInfo<ViewFolderBO> queryPage(ViewFolderBO bo) {
        PageHelper.startPage(bo.getPageNumber(), bo.getPageSize());
        return new PageInfo<>(viewFolderDao.queryList(bo));
    }
    
    @WriteTransactional
    public int save(ViewFolderBO bo) {
        if (Tool.isNotBlank(bo.getId())) {
            bo.setUpdateTime(System.currentTimeMillis());
            return viewFolderDao.update(bo);
        } else {
            bo.setId(UUID.getUUID());
            bo.setUpdateTime(System.currentTimeMillis());
            bo.setCreateTime(System.currentTimeMillis());
            return viewFolderDao.insert(bo);
        }
    }

    @WriteTransactional
    public void saveList(List<ViewFolderBO> folderList) {
        if (folderList == null) return;

        folderList.forEach(folder -> {
            if (viewFolderDao.queryBy(folder.getId()) != null) {
                folder.setUpdateTime(System.currentTimeMillis());
                viewFolderDao.update(folder);
            } else {
                folder.setUpdateTime(System.currentTimeMillis());
                folder.setCreateTime(System.currentTimeMillis());
                viewFolderDao.insert(folder);
            }
        });
    }
    
    @WriteTransactional
    public int delete(ViewFolderBO bo) {
        return viewFolderDao.delete(bo);
    }

    public long queryTotal(ViewFolderBO bo) {
        PageHelper.startPage(0, 0);
        return new PageInfo<>(viewFolderDao.queryList(bo)).getTotal();
    }


    @WriteTransactional
    public int updateParentId(ViewFolderBO bo) {
        return viewFolderDao.updateParentId(bo);
    }

    public boolean isParent(String id, String parentId) {
        if (Tool.isBlank(parentId) || Constants.ROOT_ID.equals(parentId)) {
            return false;
        }

        if (Tool.isBlank(id) || Constants.ROOT_ID.equals(id)) {
            return false;
        }

        ViewFolderBO bo = viewFolderDao.queryBy(id);
        if (bo == null) {
            return false;
        } else if (bo.getParentId().equals(parentId)) {
            return true;
        }

        return isParent(bo.getParentId(), parentId);
    }
}
